/***********************************************************************************************************************
 * Copyright (c) 2010 Ronald D. Kurr. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under th License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 **********************************************************************************************************************/

/***********************************************************************************************************************
 * Copyright (c) 2010 Ronald D. Kurr. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under th License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 **********************************************************************************************************************/

/***********************************************************************************************************************
 * Copyright (c) 2010 Ronald D. Kurr. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under th License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 **********************************************************************************************************************/

/***********************************************************************************************************************
 * Copyright (c) 2010 Ronald D. Kurr. Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
 * distributed under th License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 **********************************************************************************************************************/

package org.kurron.web.service;

import java.net.URL;
import org.springframework.stereotype.Service;

/**
 * This object will use Flickr's URL shortening base 58 algorithm to generate an alias.
 */
@Service
public final class Base58AliasGenerator implements AliasGenerator
{
    private static final char[] BASE58_CHARS = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray();

    @Override
    public String generateAlias( final URL original )
    {
        return numberToAlpha( System.identityHashCode( original ) );
    }

    private String numberToAlpha( long number )
    {
        final char[] buffer = new char[20];
        int index = 0;
        do
        {
            buffer[index++] = BASE58_CHARS[(int) (number % BASE58_CHARS.length)];
            number = number / BASE58_CHARS.length;
        }
        while( number > 0 );
        return new String( buffer, 0, index );
    }

    @SuppressWarnings( {"UnusedDeclaration"} )
    private long alphaToNumber( final String text )
    {
        char[] chars = text.toCharArray();
        long result = 0;
        long multiplier = 1;
        for( char c : chars )
        {
            int digit;
            if( c >= '1' && c <= '9' )
            {
                digit = c - '1';
            }
            else if( c >= 'A' && c < 'I' )
            {
                digit = (c - 'A') + 9;
            }
            else if( c > 'I' && c < 'O' )
            {
                digit = (c - 'J') + 17;
            }
            else if( c > 'O' && c <= 'Z' )
            {
                digit = (c - 'P') + 22;
            }
            else if( c >= 'a' && c < 'l' )
            {
                digit = (c - 'a') + 33;
            }
            else if( c > 'l' && c <= 'z' )
            {
                digit = (c - 'l') + 43;
            }
            else
            {
                throw new IllegalArgumentException( "Illegal character found: '" + c + "'" );
            }

            result += digit * multiplier;
            multiplier = multiplier * BASE58_CHARS.length;
        }
        return result;
    }
}